<?php 
/**
* 数据操作类
*/
class Model
{
	public $configs= array(
		'DB_HOST'=>'127.0.0.1',
		'DB_DATABASES'=>'bbs',
		'DB_USER'=>'root',
		'DB_PWD'=>'',
		'DB_CODE'=>'utf8'
		);
	public $table;
	public $where;
	public $orderby;
	public $limit;
	public static $link;
	public function __construct($table='')
	{
		$this->table = !empty($table) ? $table : '';

		// var_dump($this->table);

		if(!self::$link){
			$this->configs['DB_HOST'] = C('DB_HOST')?C('DB_HOST'):$this->configs['DB_HOST'];
			$this->configs['DB_DATABASES'] = C('DB_DATABASES')?C('DB_DATABASES'):$this->configs['DB_DATABASES'];
			$this->configs['DB_USER'] = C('DB_USER')?C('DB_USER'):$this->configs['DB_USER'];
			$this->configs['DB_PWD'] = C('DB_PWD')?C('DB_PWD'):$this->configs['DB_PWD'];
			$this->configs['DB_CODE'] = C('DB_CODE')?C('DB_CODE'):$this->configs['DB_CODE'];

			$dsn = "mysql:host=".$this->configs['DB_HOST'].";dbname=".$this->configs['DB_DATABASES'];

			try{
				self::$link = new Pdo($dsn,$this->configs['DB_USER'],$this->configs['DB_PWD']);

			}catch(PDOException $e) {
				exit($e->getMessage());
			}

			self::$link->query("set names ".$this->configs['DB_CODE']);
		}
	}

	public function query($sql)
	{
		$res = self::$link->query($sql);
		if(!$res){
			throw new Exception("Error Processing Request:".self::$link->errorinfo()[2], 1);
		}
		return $res->fetchAll(PDO::FETCH_ASSOC);
	}

	public function add($arr)
	{
		$keys = implode(',', array_keys($arr)); 
		$values = implode("','", array_values($arr));

		$sql = "insert into ".$this->table."(".$keys.") value('".$values."')";

		$res = self::$link->exec($sql);

		if($res){
			return self::$link->lastInsertId();
		}else{

		}
	}

	public function del()
	{
		$this->link;
	}

	public function update()
	{
		$this->link;
	}

	public function where($data)
	{
		$this->where=" where ".$data;
		return $this;
	}

	public function orderby($data)
	{
		$this->orderby=" order by ".$data;
		return $this;
	}
	public function limit($data)
	{
		$this->limit=" limit ".$data;
		return $this;
	}

	public function select($data='*')
	{
		$sql = "SELECT ".$data." FROM ".$this->table.$this->where.$this->orderby.$this->limit;

		return $this->query($sql);
	}
}










 ?>